IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSPCopyFile]') AND type in (N'P', N'PC'))
BEGIN
     DROP PROCEDURE [dbo].[MSPCopyFile]
END
GO

CREATE PROCEDURE [dbo].[MSPCopyFile]
	@SourceFilePath [nvarchar](4000),
	@SourceFileName [nvarchar](4000),
	@DestinationFilePath [nvarchar](4000),
	@DestinationFileName [nvarchar](4000),
	@OverwriteExistingFileFlag [tinyint]
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [FileManipulationSQLCLRExternal].[FileManipulationSQLCLRExternal.StoredProcedures].[MSPCopyFile]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSPDeleteFile]') AND type in (N'P', N'PC'))
BEGIN
     DROP PROCEDURE [dbo].[MSPDeleteFile]
END
GO

CREATE PROCEDURE [dbo].[MSPDeleteFile]
	@FilePath [nvarchar](4000),
	@FileName [nvarchar](4000)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [FileManipulationSQLCLRExternal].[FileManipulationSQLCLRExternal.StoredProcedures].[MSPDeleteFile]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSPDeleteFiles]') AND type in (N'P', N'PC'))
BEGIN
     DROP PROCEDURE [dbo].[MSPDeleteFiles]
END
GO

CREATE PROCEDURE [dbo].[MSPDeleteFiles]
	@FilePath [nvarchar](4000),
	@FilePattern [nvarchar](4000)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [FileManipulationSQLCLRExternal].[FileManipulationSQLCLRExternal.StoredProcedures].[MSPDeleteFiles]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSPGetFileSpecs]') AND type in (N'P', N'PC'))
BEGIN
     DROP PROCEDURE [dbo].[MSPGetFileSpecs]
END
GO

CREATE PROCEDURE [dbo].[MSPGetFileSpecs]
	@FilePath [nvarchar](4000),
	@FileName [nvarchar](4000),
	@FileSize [int] OUTPUT,
	@FileDt [datetime] OUTPUT,
	@FileExistFlag [tinyint] OUTPUT
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [FileManipulationSQLCLRExternal].[FileManipulationSQLCLRExternal.StoredProcedures].[MSPGetFileSpecs]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSPMakeDirectory]') AND type in (N'P', N'PC'))
BEGIN
     DROP PROCEDURE [dbo].[MSPMakeDirectory]
END
GO

CREATE PROCEDURE [dbo].[MSPMakeDirectory]
	@FullDirectoryPath [nvarchar](4000)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [FileManipulationSQLCLRExternal].[FileManipulationSQLCLRExternal.StoredProcedures].[MSPMakeDirectory]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSPMoveFile]') AND type in (N'P', N'PC'))
BEGIN
     DROP PROCEDURE [dbo].[MSPMoveFile]
END
GO

CREATE PROCEDURE [dbo].[MSPMoveFile]
	@SourceFilePath [nvarchar](4000),
	@SourceFileName [nvarchar](4000),
	@DestinationFilePath [nvarchar](4000),
	@DestinationFileName [nvarchar](4000),
	@OverwriteExistingFileFlag [tinyint]
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [FileManipulationSQLCLRExternal].[FileManipulationSQLCLRExternal.StoredProcedures].[MSPMoveFile]
GO

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[MSPSaveFileImage]') AND type in (N'P', N'PC'))
BEGIN
     DROP PROCEDURE [dbo].[MSPSaveFileImage]
END
GO

CREATE PROCEDURE [dbo].[MSPSaveFileImage]
	@FilePath [nvarchar](4000),
	@FileName [nvarchar](4000),
	@FileBytes [varbinary](max)
WITH EXECUTE AS CALLER
AS
EXTERNAL NAME [FileManipulationSQLCLRExternal].[FileManipulationSQLCLRExternal.StoredProcedures].[MSPSaveFileImage]
GO
